package com.suyeer.consumer.util;

import com.suyeer.basic.util.LogUtil;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by jun on 2017/8/4.
 */
@Component
public class CacheUtil {

    public final static ConcurrentHashMap<String, Long> cacheTime = new ConcurrentHashMap<>();

    /**
     * User: jun
     * Date: 2017/8/7
     * Desc: 设置MC的标记时间
     */
    public static void setNextUpdateTimeToMem(String key) {
        try {
            cacheTime.put(key, System.currentTimeMillis());
        } catch (Exception e) {
            LogUtil.error(e.getMessage());
        }
    }

    /**
     * User: jun
     * Date: 2017/8/7
     * Desc: 判断缓存是否更新
     */
    public static Boolean isNeedInit(String key, long lastTime, boolean isEmpty) {
        boolean flag = true;
        try {
            Long mcTime = cacheTime.get(key);
            if (mcTime != null && !isEmpty) {
                flag = mcTime.longValue() > lastTime;
            }
            if (mcTime == null) {
                setNextUpdateTimeToMem(key);
            }
        } catch (Exception e) {
            LogUtil.error("读取MC失败, 默认需要更新: {}!", e.getMessage());
        } finally {
            return flag;
        }
    }
}
